<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="这个项目是很早之前在学校做的，如今再回首。很多代码很是粗糙，逻辑也不尽完善。还望各位看官海涵。  前言通过上一篇文章的功能设计，我们可以发现新闻通知公告等是APP的最主要功能点。主要是聚合展示学校官网的通知公告，官方微信，微博的重要信息，学校社团的活动，以及学校优秀自媒体的文章等等。 系列文章:  手把手带你撸一个校园APP（一）：项目简介 手把手带你撸一个校园APP（二）：应用启动和欢迎页面">
<meta property="og:type" content="article">
<meta property="og:title" content="手把手带你撸一个校园APP（五）：新闻中心模块">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%BA%94%EF%BC%89%EF%BC%9A%E6%96%B0%E9%97%BB%E4%B8%AD%E5%BF%83%E6%A8%A1%E5%9D%97/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="这个项目是很早之前在学校做的，如今再回首。很多代码很是粗糙，逻辑也不尽完善。还望各位看官海涵。  前言通过上一篇文章的功能设计，我们可以发现新闻通知公告等是APP的最主要功能点。主要是聚合展示学校官网的通知公告，官方微信，微博的重要信息，学校社团的活动，以及学校优秀自媒体的文章等等。 系列文章:  手把手带你撸一个校园APP（一）：项目简介 手把手带你撸一个校园APP（二）：应用启动和欢迎页面">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200208211236360.gif">
<meta property="article:published_time" content="2021-08-25T14:42:51.207Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.809Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="科师有约">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200208211236360.gif">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%BA%94%EF%BC%89%EF%BC%9A%E6%96%B0%E9%97%BB%E4%B8%AD%E5%BF%83%E6%A8%A1%E5%9D%97/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>手把手带你撸一个校园APP（五）：新闻中心模块 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%BA%94%EF%BC%89%EF%BC%9A%E6%96%B0%E9%97%BB%E4%B8%AD%E5%BF%83%E6%A8%A1%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          手把手带你撸一个校园APP（五）：新闻中心模块
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>这个项目是很早之前在学校做的，如今再回首。很多代码很是粗糙，逻辑也不尽完善。还望各位看官海涵。</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>通过上一篇文章的功能设计，我们可以发现新闻通知公告等是APP的最主要功能点。主要是聚合展示学校官网的通知公告，官方微信，微博的重要信息，学校社团的活动，以及学校优秀自媒体的文章等等。</p>
<p><strong>系列文章:</strong></p>
<ol>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/80787263">手把手带你撸一个校园APP（一）：项目简介</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81456617">手把手带你撸一个校园APP（二）：应用启动和欢迎页面</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81837608">手把手带你撸一个校园APP（三）：用户模块（登录注册等）</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104209803">手把手带你撸一个校园APP（四）：APP框架及功能设计</a></li>
<li>手把手带你撸一个校园APP（五）：新闻中心模块</li>
<li>手把手带你撸一个校园APP（六）：失物招领&amp;二手交易模块</li>
<li>手把手带你撸一个校园APP（七）：图说校园模块</li>
<li>手把手带你撸一个校园APP（八）：校园通讯录模块</li>
<li>手把手带你撸一个校园APP（九）：课程表模块（模拟登陆爬取教务处课程信息）</li>
<li>手把手带你撸一个校园APP（十）：APP通用模块（更新，意见反馈等）</li>
</ol>
<h2 id="实现效果"><a href="#实现效果" class="headerlink" title="实现效果"></a>实现效果</h2><p>新闻部分主要是分了“通知公告”，“新闻速递”，“自媒体”，“校园文化”四个栏目，之间可以通过左右滑动进行切换。效果图如下：<br><img src="https://img-blog.csdnimg.cn/20200208211236360.gif" alt="科师有约新闻模块"></p>
<h2 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h2><p>其实这是挺常见的一个新闻资讯模块。我们也可以轻易得出以下内容：</p>
<ol>
<li>新闻条目 一般都有标题，配图，作者，发表时间等组成。我们的新闻只是聚合展示使用，可以外加一个资讯链接；</li>
<li>配图数量并不一致，有无图的，1 张配图的，以及 3 张配图的。我们需要根据配图的数量进行不同布局的设置；</li>
<li>新闻列表应该拥有下拉刷新，上拉加载的功能。</li>
</ol>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><p>本项目基于Bmob进行开发，很多地方需要Bmob相关知识。大家如果没了解过的话，可先自行查看 <a target="_blank" rel="noopener" href="http://doc.bmob.cn/data/android/develop_doc/">Bmob开发文档</a> 。</p>
<h3 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h3><p>当初设计数据库字段的时候，很多东西都为了简单，没有做更合理的规划。字段如下：<br>| 字段名            | 描述   | 类型    | 主键 |<br>| —————– | —— | ——- | —- |<br>| objectId          | 用户id | String  | 是   |<br>| title | 标题| String  |      |<br>| author| 作者 | String  |      |<br>| time              | 发布时间| String  |      |<br>|kind|栏目分类|String||<br>| content| 内容链接| String  |      |<br>|pic1             | 缩略图一| String  |      |<br>| pic2               | 缩略图二   | String  |      |<br>| pic3             | 缩略图三  | String  |      |<br>| tag              | 布局样式（图片数量）| String  |      |</p>
<blockquote>
<p>大家如果自己从头来的话，建议适当更改部分字段的类型以及组合方式，会更合理些。比如time改为时间类型，图片改为列表等。本文暂且不表。</p>
</blockquote>
<h3 id="安卓"><a href="#安卓" class="headerlink" title="安卓"></a>安卓</h3><h4 id="逻辑流程讲解"><a href="#逻辑流程讲解" class="headerlink" title="逻辑流程讲解"></a>逻辑流程讲解</h4><p>本模块逻辑设计如下：</p>
<ol>
<li>使用CoordinatorTabLayout + ViewPager + Fragment 完成新闻模块主页面的显示；</li>
<li>Fragment 进行网络请求，获取新闻数据并展示，使用 RecyclerView 展示新闻列表，根据缩略图数量对应3种条目布局；</li>
<li>新闻详情页面使用WebView 用于展示新闻详情。</li>
</ol>
<h4 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h4><p>在这里为了能快速开发，我们使用了 CoordinatorTabLayout 。</p>
<blockquote>
<p>文章：<a target="_blank" rel="noopener" href="http://hugeterry.cn/dreams/559">CoordinatorTabLayout组件库</a><br>Github地址：<a target="_blank" rel="noopener" href="https://github.com/hugeterry/CoordinatorTabLayout">CoordinatorTabLayout</a><br> 作者：hugeterry</p>
</blockquote>
<h5 id="使用-CoordinatorTabLayout-搭建新闻主页面"><a href="#使用-CoordinatorTabLayout-搭建新闻主页面" class="headerlink" title="使用 CoordinatorTabLayout 搭建新闻主页面"></a>使用 CoordinatorTabLayout 搭建新闻主页面</h5><p>使用步骤如下：</p>
<ol>
<li><p>在 app 的 build.gradle 中添加依赖</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">dependencies</span> &#123;</span><br><span class="line">    implementation <span class="string">&#x27;cn.hugeterry.coordinatortablayout:coordinatortablayout:1.2.2&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p> 布局文件中使用</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">RelativeLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">cn.hugeterry.coordinatortablayout.CoordinatorTabLayout</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/cootablayout_news&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">android.support.v4.view.ViewPager</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">&quot;@+id/vp_news&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">app:layout_behavior</span>=<span class="string">&quot;@string/appbar_scrolling_view_behavior&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">cn.hugeterry.coordinatortablayout.CoordinatorTabLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">RelativeLayout</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>在 java 代码中使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 四个栏目标题</span></span><br><span class="line">String[] mTitles = &#123;<span class="string">&quot;通知公告&quot;</span>, <span class="string">&quot;新闻速递&quot;</span>, <span class="string">&quot;自媒体&quot;</span>, <span class="string">&quot;校园文化&quot;</span>&#125;</span><br><span class="line"><span class="comment">// 初始化 Fragment 列表</span></span><br><span class="line">ArrayList&lt;Fragment&gt; mFragments = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">mFragments.add(MainFragment1.getInstance(mTitles[<span class="number">0</span>]));</span><br><span class="line">mFragments.add(MainFragment2.getInstance(mTitles[<span class="number">1</span>]));</span><br><span class="line">mFragments.add(MainFragment3.getInstance(mTitles[<span class="number">2</span>]));</span><br><span class="line">mFragments.add(MainFragment4.getInstance(mTitles[<span class="number">3</span>]));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置Viewpager</span></span><br><span class="line">mViewPager.setOffscreenPageLimit(<span class="number">4</span>);</span><br><span class="line">mViewPager.setAdapter(<span class="keyword">new</span> MyPagerAdapter(getChildFragmentManager(), mFragments, mTitles));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置顶部 展开后 4 张背景图</span></span><br><span class="line">mImageArray = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;</span><br><span class="line">        R.mipmap.keshi1,</span><br><span class="line">        R.mipmap.keshi4,</span><br><span class="line">        R.mipmap.img_bg_news3,</span><br><span class="line">        R.mipmap.img_bg_news4&#125;;</span><br><span class="line"><span class="comment">// 设置顶部 收缩后 4 种背景色</span></span><br><span class="line">mColorArray = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;</span><br><span class="line">        android.R.color.holo_blue_light,</span><br><span class="line">        android.R.color.holo_red_light,</span><br><span class="line">        android.R.color.holo_orange_light,</span><br><span class="line">        android.R.color.holo_green_light&#125;;</span><br><span class="line"><span class="comment">// 设置 CoordinatorTabLayout 属性</span></span><br><span class="line">mCoordinatorTabLayout.setTransulcentStatusBar(mActivity)</span><br><span class="line">        .setTitle(<span class="string">&quot;&quot;</span>)</span><br><span class="line">        .setImageArray(mImageArray, mColorArray)</span><br><span class="line">        .setupWithViewPager(mViewPager);</span><br></pre></td></tr></table></figure></li>
</ol>
<h5 id="网络请求，数据填充"><a href="#网络请求，数据填充" class="headerlink" title="网络请求，数据填充"></a>网络请求，数据填充</h5><ol>
<li>建立数据库对应表 bean 类<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NewsBean</span> <span class="keyword">extends</span> <span class="title">BmobObject</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String title;</span><br><span class="line">    <span class="keyword">private</span> String author;</span><br><span class="line">    <span class="keyword">private</span> String time;</span><br><span class="line">    <span class="keyword">private</span> String tag;</span><br><span class="line">    <span class="keyword">private</span> String pic1;</span><br><span class="line">    <span class="keyword">private</span> String pic2;</span><br><span class="line">    <span class="keyword">private</span> String pic3;</span><br><span class="line">    <span class="keyword">private</span> String content;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// ....get set 方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>网络请求数据，并填充<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">mAdapter = <span class="keyword">new</span> NNNAdapter();</span><br><span class="line">newsBeanList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">BmobQuery&lt;NewsBean&gt; query = <span class="keyword">new</span> BmobQuery&lt;NewsBean&gt;();</span><br><span class="line">query.order(<span class="string">&quot;-time&quot;</span>);</span><br><span class="line"><span class="comment">// kind: [a=&quot;通知公告&quot;, b=&quot;新闻速递&quot;, c=&quot;自媒体&quot;, d=&quot;校园文化&quot;]</span></span><br><span class="line">query.addWhereEqualTo(<span class="string">&quot;kind&quot;</span>, <span class="string">&quot;a&quot;</span>);</span><br><span class="line">query.setLimit(<span class="number">10</span>);</span><br><span class="line">query.findObjects(<span class="keyword">new</span> FindListener&lt;NewsBean&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">done</span><span class="params">(List&lt;NewsBean&gt; object, BmobException e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (e == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">for</span> (NewsBean newsBean : object) &#123;</span><br><span class="line">                newsBeanList.add(newsBean);</span><br><span class="line">                LogUtils.e(newsBean.getAuthor());</span><br><span class="line">            &#125;</span><br><span class="line">            mAdapter.setData(newsBeanList);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">mRecyclerView.setAdapter(mAdapter);</span><br></pre></td></tr></table></figure></li>
<li>图片数量不同对应不同的布局<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NewsBean</span> <span class="keyword">extends</span> <span class="title">BmobObject</span> <span class="keyword">implements</span> <span class="title">IMultiItem</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getLayoutRes</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">&quot;3&quot;</span>.equals(tag)) &#123;</span><br><span class="line">            <span class="keyword">return</span> R.layout.item_news_type3;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">&quot;2&quot;</span>.equals(tag)) &#123;</span><br><span class="line">            <span class="keyword">return</span> R.layout.item_news_type2;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> R.layout.item_news_type1;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略其它...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>新闻详情页面<br>Fragment 中给 RecycleView 设置点击事件，传递网址到详情Activity<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">mAdapter.setOnItemClickListener(<span class="keyword">new</span> OnItemClickListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemClick</span><span class="params">(<span class="meta">@NonNull</span> View view, <span class="keyword">int</span> position)</span> </span>&#123;</span><br><span class="line">        Intent intent = <span class="keyword">new</span> Intent(getActivity(), NewsWebActivity.class);</span><br><span class="line">        intent.putExtra(<span class="string">&quot;url&quot;</span>, newsBeanList.get(position).getContent());</span><br><span class="line">        startActivity(intent);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
网页详情页面 NewsActivity 中只有一个WebView。接收上个页面传递过来的参数，并且展示页面。这里做了一些初始化操作，大家在别处需要的话，也可以直接复制过去<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">String url = getIntent().getStringExtra(<span class="string">&quot;url&quot;</span>);</span><br><span class="line"><span class="keyword">if</span> (TextUtils.isEmpty(url)) &#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span>, <span class="string">&quot;该新闻已经失效！&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line">MyWebViewClient myWebViewClient = <span class="keyword">new</span> MyWebViewClient();</span><br><span class="line"><span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) &#123;</span><br><span class="line">    wvNews.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);</span><br><span class="line">&#125;</span><br><span class="line">wvNews.getSettings().setJavaScriptCanOpenWindowsAutomatically(<span class="keyword">true</span>);<span class="comment">//设置js可以直接打开窗口，如window.open()，默认为false</span></span><br><span class="line">wvNews.getSettings().setJavaScriptEnabled(<span class="keyword">true</span>);<span class="comment">//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞</span></span><br><span class="line">wvNews.getSettings().setSupportZoom(<span class="keyword">true</span>);<span class="comment">//是否可以缩放，默认true</span></span><br><span class="line">wvNews.getSettings().setBuiltInZoomControls(<span class="keyword">true</span>);<span class="comment">//是否显示缩放按钮，默认false</span></span><br><span class="line">wvNews.getSettings().setUseWideViewPort(<span class="keyword">true</span>);<span class="comment">//设置此属性，可任意比例缩放。大视图模式</span></span><br><span class="line">wvNews.getSettings().setLoadWithOverviewMode(<span class="keyword">true</span>);<span class="comment">//和setUseWideViewPort(true)一起解决网页自适应问题</span></span><br><span class="line">wvNews.getSettings().setAppCacheEnabled(<span class="keyword">true</span>);<span class="comment">//是否使用缓存</span></span><br><span class="line">wvNews.getSettings().setDomStorageEnabled(<span class="keyword">true</span>);<span class="comment">//DOM Storage</span></span><br><span class="line">wvNews.setWebChromeClient(<span class="keyword">new</span> WebChromeClient());</span><br><span class="line">wvNews.setWebViewClient(myWebViewClient);</span><br><span class="line">wvNews.loadUrl(url);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ....</span></span><br><span class="line"><span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">MyWebViewClient</span> <span class="keyword">extends</span> <span class="title">WebViewClient</span> </span>&#123;</span><br><span class="line">    <span class="comment">//重写父类方法，让新打开的网页在当前的WebView中显示</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">shouldOverrideUrlLoading</span><span class="params">(WebView view, String url)</span> </span>&#123;</span><br><span class="line">        view.loadUrl(url);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<p>至此，新闻模块页面基本完成。还有就是本文挺长的，很感谢你可以看到这里。</p>
<p>如果本文对你有所帮助，还望可以随手赏一个点赞哈 ~ ~</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E7%A7%91%E5%B8%88%E6%9C%89%E7%BA%A6/" rel="tag"># 科师有约</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E5%BA%94%E7%94%A8%E5%90%AF%E5%8A%A8%E5%92%8C%E6%AC%A2%E8%BF%8E%E9%A1%B5%E9%9D%A2/" rel="prev" title="手把手带你撸一个校园APP（二）：应用启动和欢迎页面">
      <i class="fa fa-chevron-left"></i> 手把手带你撸一个校园APP（二）：应用启动和欢迎页面
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%85%AB%EF%BC%89%EF%BC%9A%E6%A0%A1%E5%9B%AD%E9%80%9A%E8%AE%AF%E5%BD%95%E6%A8%A1%E5%9D%97/" rel="next" title="手把手带你撸一个校园APP（八）：校园通讯录模块">
      手把手带你撸一个校园APP（八）：校园通讯录模块 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C"><span class="nav-number">2.</span> <span class="nav-text">实现效果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E6%9E%90"><span class="nav-number">3.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">4.1.</span> <span class="nav-text">数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E5%8D%93"><span class="nav-number">4.2.</span> <span class="nav-text">安卓</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%80%BB%E8%BE%91%E6%B5%81%E7%A8%8B%E8%AE%B2%E8%A7%A3"><span class="nav-number">4.2.1.</span> <span class="nav-text">逻辑流程讲解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.2.2.</span> <span class="nav-text">代码实现</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-CoordinatorTabLayout-%E6%90%AD%E5%BB%BA%E6%96%B0%E9%97%BB%E4%B8%BB%E9%A1%B5%E9%9D%A2"><span class="nav-number">4.2.2.1.</span> <span class="nav-text">使用 CoordinatorTabLayout 搭建新闻主页面</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%BD%91%E7%BB%9C%E8%AF%B7%E6%B1%82%EF%BC%8C%E6%95%B0%E6%8D%AE%E5%A1%AB%E5%85%85"><span class="nav-number">4.2.2.2.</span> <span class="nav-text">网络请求，数据填充</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
